#include <allegro.h>
#include <winalleg.h>
#include <time.h>
#include <deque>
#include "gra.h"
#include "apple.h"
#include "waz.h"
#include "menu.h"
using namespace std;
time_t t;


int main()
{
    gra * game = new gra();
    waz * snake = new waz();
    apple * owoc = new apple();
    menu * men = new menu();
    int r=540;
    men->y=1;
    game->timer=0;
    srand((unsigned)time(&t));

    //  game.pozycjaX={540,500,460,420,380,340,300,260,220,180,140,100,60};
    //  game.pozycjaY={540,500,460,420,380,340,300,260,220,180,140,100,60};
    for(int i=0;i<=12;i++)
    {
        owoc->pozycjaX[i]=r;
        owoc->pozycjaY[i]=r;
        r-=40;
    }

    game->init(snake, owoc, men);
    do
    {
        men->wysMenu(game->bufor);
        men->steruj();
    }while( !key[ KEY_ENTER]);
    if(men->y==1)
    {
        clear_bitmap(game->bufor);

        game->draw(snake, owoc);
        owoc->losuj(snake, game->bufor);

        do
        {
            if( key[ KEY_LEFT ] )
            {
                while(!(key[ KEY_UP ]) && !(key[ KEY_DOWN ])&& !(key[ KEY_ESC ]) && !(game->przegrana(snake)))
                {
                    snake->x-=20;
                    game->draw(snake, owoc);
                    snake->b=1;
                }
            }

            if( key[ KEY_RIGHT ] )
            {
                while( !(key[ KEY_UP ]) && !(key[ KEY_DOWN ])&& !(key[ KEY_ESC ]) && !(game->przegrana(snake)))
                {
                    snake->x+=20;
                    game->draw(snake, owoc);
                    snake->b=2;

                }
            }

            if( key[ KEY_UP ] )
            {
                while(!(key[ KEY_RIGHT ]) &&  !(key[ KEY_LEFT ]) && !(key[ KEY_ESC ])&& !( game->przegrana(snake)))
                {
                    snake->y-=20;
                    game->draw(snake, owoc);
                    snake->b=3;

                }
            }

            if( key[ KEY_DOWN ] )
            {
                while(!(key[ KEY_RIGHT ]) &&  !(key[ KEY_LEFT ]) && !(key[ KEY_ESC ])&& !( game->przegrana(snake)))
                {
                    snake->y+=20;
                    game->draw(snake, owoc);
                    snake->b=4;
                }
            }
        }while( !key[ KEY_ESC ] && !game->przegrana(snake));
        allegro_message( "Przegrales !" );
        allegro_exit();
    }
    else if(men->y==2)
    {
        allegro_exit();
    }
    return 0;
}
END_OF_MAIN();

